<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%>
<%
	String path=request.getContextPath();
	String basePath=request.getScheme()+"://"
			+request.getServerName()+":"+request.getServerPort()
			+path+"/";
%>
<!DOCTYPE html>
<html>
<base href="<%=basePath%>">
<head>
	<title>公告管理</title>
	<meta charset="utf-8">
	<link rel="icon" href="img/logo.png" sizes="32x32">
	<link rel="stylesheet" type="text/css" href="layui/css/layui.css">
	<script type="text/javascript" src="layui/layui.js"></script>
</head>
<body>
	<c:if test="${addFlag }">
	<script type="text/javascript">
		layui.use('layer', function() {
			var layer = layui.layer;
			layer.msg('添加成功', { icon: 1, time: 1000, shade: [0.6, '#000', true] });
		});
	</script>
	</c:if>
	<div class="layui-anim layui-anim-up">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend>发布公告</legend>
		</fieldset>
		<form class="layui-form" action="admin/noticeList?addFlag=true" id="form">
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label" style="width: 40px;">内容</label>
				<div class="layui-input-block" style="margin-left: 70px;">
					<textarea placeholder="请输入公告内容(不得超过20字)" class="layui-textarea" id="noticeContent" name="noticeContent"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
			    <div class="layui-input-block">
			      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">发布</button>
			      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
			    </div>
			</div>
		</form>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend>公告列表</legend>
		</fieldset>
		<table class="layui-hide" id="test" lay-filter="demo"></table>
		<script type="text/html" id="barDemo">
	  		<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
	  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>
	</div>
	<script>
		layui.use('table', function(){
		  var table = layui.table;
		  var $=layui.$;
		  table.render({
		    elem: '#test'
		    ,url:'notice/list'
		    ,cols: [[
		      {field:'noticeId', width:'10%', title: '编号', sort: true}
		      ,{field:'noticeContent', width:'40%', title: '公告内容',edit: 'text'}
		      ,{field:'noticeDate', width:'20%', title: '发布日期', sort: true}
		      ,{fixed:'right', title:'操作', toolbar: '#barDemo', width:'30%'}
		    ]]
		    ,page: true
		  });
		  table.on('tool(demo)', function(obj){
			    var data = obj.data;
			   	if(obj.event === 'del'){
			      layer.confirm('真的删除么', function(index){
			      	obj.del();
			        layer.close(index);
			        $.ajax({
			            type : "POST",
			            dataType:"json",
			            contentType: "application/json;charset=UTF-8",
			            url : "notice/delete",
			            async: false,
			            data : JSON.stringify(data),
			            success : function(result) {
			            	
			            },
			            error : function(e){
			                console.log(e.status);
			                console.log(e.responseText);
			            }
			        });
			      });
			    } else if(obj.event === 'edit'){
			    	 table.on('edit(demo)', function(obj){
			 		    var value = obj.value //得到修改后的值
			 		    ,data = obj.data //得到所在行所有键值
			 		    ,field = obj.field; //得到字段
			 		   // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
			 		 });
			    	 layer.confirm('确认修改吗', function(index){
					        layer.close(index);
					        $.ajax({
					            type : "POST",
					            dataType:"json",
					            contentType: "application/json;charset=UTF-8",
					            url : "notice/update",
					            async: false,
					            data : JSON.stringify(data),
					            success : function(result) {
					            	
					            },
					            error : function(e){
					                console.log(e.status);
					                console.log(e.responseText);
					            }
					        });
					   });
			    	}
			  });
		});
	</script>
	<script type="text/javascript">
		layui.use('layer', function() {
			var layer = layui.layer;
			var $ = layui.jquery;
			//表单验证
			$("#form").submit(function() {
				if($("#noticeContent").val()==""){
					layer.msg('不能发布空内容', { icon: 2, time: 1000, shade: [0.6, '#000', true] });
					return false;
				}
				var data={noticeContent:$("#noticeContent").val()}
				$.ajax({
		            type : "POST",
		            dataType:"json",
		            contentType: "application/json;charset=UTF-8",
		            url : "notice/add",
		            async: false,
		            data : JSON.stringify(data),
		            success : function(result) {
		            	layer.msg('修改成功', { icon: 1, time: 1000, shade: [0.6, '#000', true] });
		            },
		            error : function(e){
		                console.log(e.status);
		                console.log(e.responseText);
		            }
		        });
				return true;
			});
		});
	</script>
</body>
</html>

